import { defineStore } from "pinia"
import { ref, computed } from "vue"
import type { Candidate, Filter } from "../types"

export const useCandidatesStore = defineStore("candidates", () => {
  const candidates = ref<Candidate[]>([
    {
      id: "1",
      name: "欧阳明",
      avatar: "/placeholder.svg?height=200&width=200",
      age: 35,
      gender: "男",
      experience: "10年以上",
      education: "硕士",
      university: "伦敦国王学院",
      currentPosition: "市场总监",
      currentCompany: "华为终端",
      currentLocation: "深圳",
      expectedSalary: "65K-95K",
      expectedPosition: "市场总监、CMO、营销负责人",
      expectedLocation: "上海、北京、深圳",
      expectedIndustry: "电子消费品、互联网",
      jobStatus: "在职-月内到岗",
      phone: "138****5678",
      email: "ouy****@gmail.com",
      profile: "拥有10年以上市场营销经验的资深营销专家，专注于电子消费品行业...",
      workHistory: [],
      educationHistory: [],
      projects: [],
      skills: [],
      isActive: true,
      lastActive: "刚刚活跃",
    },
  ])

  const filters = ref<Filter>({})
  const searchQuery = ref("")

  const filteredCandidates = computed(() => {
    let result = candidates.value

    if (searchQuery.value) {
      result = result.filter(
        (candidate) =>
          candidate.name.includes(searchQuery.value) ||
          candidate.currentPosition.includes(searchQuery.value) ||
          candidate.currentCompany.includes(searchQuery.value),
      )
    }

    return result
  })

  const getCandidateById = (id: string) => {
    return candidates.value.find((candidate) => candidate.id === id)
  }

  const setFilters = (newFilters: Filter) => {
    filters.value = newFilters
  }

  const setSearchQuery = (query: string) => {
    searchQuery.value = query
  }

  return {
    candidates,
    filters,
    searchQuery,
    filteredCandidates,
    getCandidateById,
    setFilters,
    setSearchQuery,
  }
})
